'''
Created on Jun 6, 2012

@author: thomas_dao
'''
from google.appengine.ext import ndb
from entity.entity import Album, AlbumLocation, EventAlbum


@ndb.tasklet
def get_by_author(author_key, **q_options):
    qry = Album.query(Album.author == author_key).order(-Album.created_time)
    data = yield qry.fetch_async(20, **q_options)
    raise ndb.Return(data)


@ndb.tasklet
def get_by_event(event_key, **q_options):
    qry = EventAlbum.query(EventAlbum.event == event_key).order(-EventAlbum.created_time)
    data = yield qry.fetch_async(20, **q_options)
    
    # albums will store all album belong to event
    albums = []
    for event_album in data:
        album_key = event_album.album
        album = yield album_key.get_async()
        albums.append(album)
        
    raise ndb.Return(albums)


@ndb.tasklet
def get_by_location(location_key, **q_options):
    qry = AlbumLocation.query(AlbumLocation.location == location_key)
    data = yield qry.fetch_async(20, **q_options)
    raise ndb.Return(data)
    
@ndb.tasklet
def get_locations(album_key, **q_options):
    qry = AlbumLocation.query(AlbumLocation.album == album_key)
    data = yield qry.fetch_async(20, **q_options)
    raise ndb.Return(data)
